package fz.vrd.ui.view.rcv;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.selection.StableIdKeyProvider;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {

    View headView,footView;

    List<T> listData ;

    FragmentActivity activity;

    protected LayoutInflater mInflater;

    RecylerViewItemClickListern listern;

    StableIdKeyProvider stableIdKeyProvider = new StableIdKeyProvider(null) ;

    public BaseRecyclerAdapter(FragmentActivity activity, List<T> listData){
        this(activity,listData,null);

        stableIdKeyProvider = new StableIdKeyProvider(null);
    }

    public BaseRecyclerAdapter(Fragment fragment, List<T> listData){
        this(fragment,listData,null);
    }


    public boolean isHaveHeadView(){

        return headView != null;
    }

    public boolean isHaveFootView(){

        return footView != null;
    }

    public BaseRecyclerAdapter(FragmentActivity activity, List<T> listData, RecylerViewItemClickListern listern){
        this.activity =activity;
        this.listData = listData;
        mInflater = LayoutInflater.from(activity);
        if(listern == null){
            if(activity instanceof RecylerViewItemClickListern ){
                this.listern = (RecylerViewItemClickListern) activity;
            }
        }else{
            this.listern = listern;
        }
    }

    public BaseRecyclerAdapter(Fragment fragment, List<T> listData, RecylerViewItemClickListern listern){
        this.activity = fragment.getActivity();
        this.listData = listData;
        mInflater = LayoutInflater.from(fragment.getContext());
        if(listern == null){
            if(fragment instanceof RecylerViewItemClickListern ){
                this.listern = (RecylerViewItemClickListern) fragment;
            }
            if(activity instanceof RecylerViewItemClickListern ){
                this.listern = (RecylerViewItemClickListern) activity;
            }
        }else{
            this.listern = listern;
        }
    }

    public List<T> getListData(){
        return  listData;
    }

    protected FragmentActivity getActivity(){

        return  activity;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == RecItemType.ITEM_TYPE_HEADER){
            return  new HeaderViewHolder(headView);
        }else if(viewType == RecItemType.ITEM_TYPE_FOOT){
            return  new FootViewHolder(footView);
        }else if(viewType == RecItemType.ITEM_TYPE_CONTENT){
            return  new RecyclerViewHolder(mInflater.inflate(getViewId(),parent,false),this,listern);
        }
        return  null;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {

        if(getItemViewType(position) == RecItemType.ITEM_TYPE_CONTENT){
            if(headView != null){
                position --;
                bindView(holder,listData.get(position));
            }else if(listData.size() > position){
                bindView(holder,(T)listData.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        int  count = listData == null ? 0 : listData.size();
        if(headView != null){
            count ++;
        }
        if(footView != null){
            count ++;
        }
        return count;
    }

    public T getItemData(int index){
        if(getListData().size() > index){
            if(headView != null){
                return listData.get(index - 1);
            }else{
                return listData.get(index);
            }
        }
        return null;
    }

    public int getContentItemCount(){
        return listData == null ? 0 : listData.size();
    }

    @Override
    public int getItemViewType(int position) {

        int dataItemCount = getContentItemCount();
        if (headView != null && position < 1) {
            //头部View
            return RecItemType.ITEM_TYPE_HEADER;
        } else if (footView != null && position >= (headView == null ? dataItemCount  : dataItemCount + 1)) {
            //底部View
            return RecItemType.ITEM_TYPE_FOOT;
        } else {
            //内容View
            return RecItemType.ITEM_TYPE_CONTENT;
        }
    }


    public void addHeadView(View view){
        this.headView = view;
    }

    public void addFootView(View view){
        this.footView = view;
    }

    public abstract int getViewId();

    public abstract void bindView(RecyclerViewHolder holder,T bean);
}
